// pages/apply-authority/index.js
const handler = require("../../utils/function.js");
const { $Message } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tip:"正在审核中",
    name:"",
    nickname:"",
    phone:"",
    address:"",
    resume:"",
    certificate: [
    ],
    video:"",
    authority_going:true,
    visible5: false,
    actions5: [
      {
        name: '取消'
      },
      {
        name: '支付',
        color: '#19be6b',
        loading: false
      }
    ],
    paymentData:[],
    is_certification: -1
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.test_type();
    this.setData({ is_apply:0})
  },
  test_type:function(){
    const that=this;
    handler.requestData("userIsApply", { user_id: wx.getStorageSync("user_info").id }, function (res) {
      console.log(res);
      res.data==2&&wx.showModal({
        content: '审核失败，请重新审核。。。',
      })
      that.setData({ is_certification:res.data});
    });
  },
  // 获取用户名
  get_name: function (e) { 
    var test_name=handler.regTest(e.detail.value,"请输入姓名");
    test_name && this.setData({ name: e.detail.value});
  },
  // 获取昵称
  get_nickname:function(e){
    var test_nickname = handler.regTest(e.detail.value, "请输入昵称");
    test_nickname && this.setData({ nickname: e.detail.value });
  },
  // 获取电话号码
  get_phone:function(e){
    var test_phone = handler.regTest(e.detail.value, "请输入正确的电话号码格式", /^1\d{10}$/);
    test_phone && this.setData({ phone: e.detail.value });
  },
  // 获取地址
  get_address:function(e){
    var test_address = handler.regTest(e.detail.value, "请输入地址");
    test_address && this.setData({ address: e.detail.value });
  },
  // 获取简介
  get_resume:function(e){
    var test_resume = handler.regTest(e.detail.value, "请输入简介");
    test_resume && this.setData({ resume: e.detail.value });
  },
  uploadsImg: function (data) {
    var imgLength = this.data.certificate.length;
    console.log(imgLength)
    if (imgLength >= 3){
      $Message({
        content: '最多上传三张证书',
        type: 'error'
      })
      return;
    }
    wx.showLoading({
      title: '上传中...',
    })
    var that = this;
    var dataImg="";
    
    for(var i=0;i<data.length;i++){
    wx.uploadFile({
      url: 'http://xyds.4hl.cn/xyds/index.php/uploadCertificate',
      filePath: data[i],
      name: 'file',
      success: function (res) {
        wx.hideLoading();
        var getArr={"src":res.data,"status":true};
        that.data.certificate.push(getArr);
        that.setData({ certificate: that.data.certificate})
      },
      fail: function (res) {
        wx.hideLoading();
        $Message({
          content: '上传失败',
          type: 'error'
        })
        
      },
      complete: function (res) {
       
      }
    })
    }
  },
  get_upload_image:function(e){
    var that = this;
    var tempArr = [];
    handler.uploadImageHandler((res) => {
      that.uploadsImg(res);
    })
    console.log(tempArr[0])
  },
  // choose_deleteImg 选择删除图片
  choose_deleteImg:function(e){
    console.log(e.currentTarget.dataset.index);
    const index = e.currentTarget.dataset.index;
    const { certificate}=this.data;
    certificate[index].status = !certificate[index].status;
    this.setData({ certificate});
    console.log(this.data.certificate);
  },
  // delete_img 删除图片
  delete_img:function(){
    const { certificate}=this.data;
    var delete_index=[];
    var img_container=[];
    const that=this;
    for (var i = 0; i < certificate.length;i++){
      certificate[i].status == false && (delete_index[delete_index.length]=certificate[i]); 
      certificate[i].status == true && (img_container[img_container.length] = certificate[i]); 
    }
    console.log(img_container);
    if (delete_index.length==0){
      $Message({
        content: '请选择要删除的图片',
        type: 'error'
      })
    }else{
      console.log(delete_index)
      wx.showModal({
        title: '',
        content: '确认是否删除？',
        success:function(e){
          if(e.confirm){
            that.setData({ certificate: img_container })
          }
         
        },
        fail:function(){}
      })
      
    }
  },
  // uploadImageHandler
  // 获取视频
  get_upload_video:function(e){
    var that=this;
    wx.showLoading({
      title: '上传中',
    })
    handler.uploadVideoHandler((res) => {
      wx.uploadFile({
        url: 'http://xyds.4hl.cn/xyds/index.php/uploadVedio',
        filePath: res,
        name: 'file',
        success: function (res) {
          var result =  JSON.parse(res.data);
          wx.hideLoading();
          if (result.success == true){
            that.setData({
              video: result.data
            })
          }else{
            $Message({
              content:  result.message,
              type: 'error'
            })
          }  
          
          
          console.log(that.data.video);
        },
        fail: function (res) {
          wx.hideLoading();
          $Message({
            content: '上传失败',
            type: 'error'
          })
          // wx.showToast({
          //   title: '上传失败',
          //   icon:'loading'
          // })
          console.log(res);
        
        }
      })
    })
  },

  submit_handler:function(){
    wx.showLoading({
      title: '提交中...',
      icon:'loading'
    })
    const { name, nickname,phone,address, resume,certificate, video}=this.data;
    // 验证信息
    var name_test=handler.regTest(name, "请输入姓名")
    var nickname_test = handler.regTest(nickname, "请输入昵称")
    var phone_test = handler.regTest(phone, "请输入电话号码", /^1\d{10}$/);
    var address_test = handler.regTest(address, "请输入地址");
    var resume_test=handler.regTest(resume,"请输入简介");
    var certificate_test=handler.regTest(certificate,"请上传资格证书");
    var video_test=handler.regTest(video,"请上传小视频");
    var test_all = name_test && nickname_test && phone_test && address_test && resume_test && certificate_test && video_test;
    var submitJson = {
      name:name,
      nick_name: nickname,
      phone:phone,
      address:address,
      introduction: resume,
      certificate: JSON.stringify(certificate),
      vedio_path: video,
      user_id:wx.getStorageSync('user_info').id,
      openid:wx.getStorageSync('user_info').openid
    };
    // debugger;
    var that = this;
    if (test_all){
      handler.requestData("userAuth",submitJson,function(res){
         wx.hideLoading();
         if(res.success == true){
           $Message({
             content:'提交成功,正在请及时缴纳认证费',
             type:'success'
           })
           that.setData({
             paymentData:res.data
           })
          //  that.setData({authority_going:false})
           that.handleOpen5()
         }else{
           $Message({
             content:res.message,
             type:"error"
           }) 
        }
      }) 

    }
     
  },
  handleOpen5() {
    this.setData({
      visible5: true
    });
  },

  handleClick5({ detail }) {
    if (detail.index === 0) {
      this.setData({
        visible5: false
      });
    } else {
      const action = [...this.data.actions5];
      // action[1].loading = true;

      this.setData({
        actions5: action
      });
      this.authPay();
    }
  },

  authPay:function(){
    var that = this;
    wx.requestPayment({
      'timeStamp': '' + this.data.paymentData.timeStamp + '',
      'nonceStr': '' + this.data.paymentData.nonceStr + '',
      'package': '' + this.data.paymentData.package + '',
      'signType': 'MD5',
      'paySign': '' + this.data.paymentData.paySign + '',
      success: function (res) {
        wx.showToast({
          title: '支付成功',
          icon: 'loading'
        })
        wx.setStorageSync("is_apply",0)
        that.setData({ visible5: false, authority_going: false})
      },
      fail: function (res) {
        wx.showToast({
          title: '支付失败',
          icon: 'loading'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})